home *** CD-ROM | disk | FTP | other *** search
/ Over 1,000 Windows 95 Programs / Over 1000 Windows 95 Programs (Microforum) (Disc 1).iso / 1138 / source.zip / POST.C < prev    next >
C/C++ Source or Header  |  1995-03-05  |  6KB  |  210 lines

  1. /*      post.c -- mechanisms for posting mail/news
  2.     This file is part of Paperboy, an offline mail/newsreader for Windows
  3.     Copyright (C) 1995  Michael H. Vartanian
  4.         vart@clark.net
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 2 of the License, or
  9.     (at your option) any later version.
  10.  
  11.     This program is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with this program; if not, write to the Free Software
  18.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <time.h>
  23. #include <stdlib.h>
  24. #include <ctype.h>
  25. /*
  26. #include <direct.h>
  27. */
  28. #include <sys\types.h>
  29. #include <sys\timeb.h>
  30. #include "soup.h"
  31. #include "error.h"
  32. #include "areas.h"
  33. #include "msgs.h"
  34.  
  35. static char datebuf[128];
  36.  
  37. char *  DLLFUNC UnixDate (void)
  38. {
  39.     time_t ltime;
  40.     struct tm *gmt;
  41.  
  42.     /* Get UNIX-style time and display as number and string. */
  43.     time( <ime );
  44.     
  45.     gmt = gmtime( <ime );
  46.     
  47.     
  48.     /* strftime (datebuf, 128, "%a, %d %b %y %H:%M:%D GMT", gmt); */
  49.     
  50.     return datebuf;
  51. }
  52.  
  53. int DLLFUNC SaveMsgToFolder (char * fname)
  54. {
  55.     FILE * fout;
  56.     unsigned long startlength, startpos, endpos, length;
  57.     int n,result;
  58.     unsigned char c;
  59.  
  60.     assert(fname!=NULL);
  61.  
  62.     fout=fopen(fname,"ab"); /* Append to file */
  63.     if (fout==NULL) { errortext="SaveMsgToFolder: Couldn't append"; return ERRIO; }
  64.     result=fseek(fout,0,SEEK_END);  /* Goto EOF, just to be sure */
  65.     if (result) { errortext="SaveMsgToFolder: Couldn't seek"; return ERRIO; }
  66.  
  67.     startlength=ftell(fout);
  68.     fputc(0xFA,fout);       /* Write dummy length */
  69.     fputc(0xCE,fout);
  70.     fputc(0xDE,fout);
  71.     fputc(0xAD,fout);
  72.  
  73.     startpos=ftell(fout);
  74.     for (n=1; n<=GetNumLines(); n++)
  75.     {
  76.         fprintf(fout,"%s",GetLine(n));
  77.         fputc(LFCHAR,fout);     /* Add LF */
  78.     }
  79.     result=fseek(fout,0,SEEK_END);  /* Goto EOF, just to be sure */
  80.     if (result) { errortext="SaveMsgToFolder: Couldn't seek"; return ERRIO; }
  81.     endpos=ftell(fout);
  82.     result=fclose(fout);
  83.     if (result) { errortext="SaveMsgToFolder: Couldn't close"; return ERRIO; }
  84.  
  85.     length=endpos-startpos; /* Calculate length of message */
  86.     
  87.     /* Write new length */
  88.     assert(length>0);
  89.     fout=fopen(fname,"r+b");        /* Random access mode */
  90.     if (fout==NULL) { errortext="SaveMsgToFolder: Couldn't open"; return ERRIO; }
  91.  
  92.     result=fseek(fout,0,SEEK_SET);  /* rewind */
  93.     if (result) { errortext="SaveMsgToFolder: Couldn't seek"; return ERRIO; }
  94.     
  95.     result=fseek(fout,startlength,SEEK_SET);
  96.     if (result) { errortext="SaveMsgToFolder: Couldn't seek"; return ERRIO; }
  97.     
  98.     c=(unsigned char)(length >> 24);
  99.     fputc(c,fout);
  100.     c=(unsigned char)(length >> 16);
  101.     fputc(c,fout);
  102.     c=(unsigned char)(length >> 8);
  103.     fputc(c,fout);
  104.     c=(unsigned char)(length >> 0);
  105.     fputc(c,fout);
  106.     result=fclose(fout);
  107.     if (result==EOF) { errortext="SaveMsgToFolder: Couldn't close"; return ERRIO; }
  108.     return 0;
  109. }
  110.  
  111. int DLLFUNC Post (char * fname,  int type)
  112. /* fname should be FNAME.MSG */
  113. {
  114.     FILE * fin, * fout;
  115.     int result;
  116.     unsigned long length;
  117.     unsigned long startpos;
  118.     unsigned char c;
  119.     char basefilename[_MAX_FNAME];
  120.     
  121.     /* Rename fname to TMPFILE, recopy onto fname sans CR */
  122.  
  123.     assert (fname!=NULL);
  124.     assert (type==POSTMAIL || type==POSTNEWS);
  125.  
  126.     result=rename(fname,TMPFILE);
  127.     if (result!=0) { errortext="Post: Couldn't rename to TMPFILE"; return ERRIO; }
  128.  
  129.     fin=fopen(TMPFILE,"rb");
  130.     if (fin==NULL) { errortext="Post: Couldn't open TMPFILE"; return ERRIO; }
  131.  
  132.     fout=fopen(fname,"wb"); 
  133.     if (fout==NULL) { errortext="Post: Couldn't write to file"; return ERRIO; }
  134.  
  135.     fseek(fout,0,SEEK_END); /* Go to EOF and remember position */
  136.     startpos=ftell(fout);
  137.  
  138.     fputc(00,fout); /* Temporary write 0x00000000 as length */
  139.     fputc(00,fout);
  140.     fputc(00,fout);
  141.     fputc(00,fout);
  142.  
  143.     /* Put in decorative headers */
  144.     /*
  145.     fprintf(fout,"X-Warning: Do not edit this file!");
  146.     fputc(LFCHAR,fout);
  147.     
  148.     if (type==POSTMAIL) 
  149.         fprintf(fout,"X-Mailer: Paperboy Version 2.02 (%s)",fname);
  150.     if (type==POSTNEWS) 
  151.         fprintf(fout,"X-Newsreader: Paperboy Version 2.02 (%s)",fname);
  152.     fputc(LFCHAR,fout);
  153.     */
  154.     /* Recopy file, stripping Carriage-return characters for Unix */
  155.     while (!feof(fin))
  156.     {
  157.         result=getc(fin);
  158.         if (result!=CRCHAR && result!=EOF) fputc(result,fout);
  159.     }
  160.  
  161.     fclose(fin);
  162.     remove(TMPFILE);                /* Nuke the temporary file */
  163.     length=ftell(fout)-4;   /* Correct for four length bytes at beginning */
  164.  
  165.     /* Write new length */
  166.     assert(length>0);
  167.     fseek(fout,startpos,SEEK_SET);
  168.     c=(unsigned char)(length  / (256L*256L*256L));
  169.     fputc(c,fout);
  170.     c=(unsigned char)(length / (256L*256L));
  171.     fputc(c,fout);
  172.     c=(unsigned char)(length / 256L);
  173.     fputc(c,fout);
  174.     c=(unsigned char)length;
  175.     fputc(c,fout);
  176.  
  177.     fclose(fout);
  178.  
  179.     /* Append to REPLIES */
  180.     fout=fopen(REPLYFILE,"ab");
  181.     
  182.     _splitpath(fname,NULL,NULL,basefilename,NULL);
  183.     /* Strip off directory and .MSG to make a prefix */
  184.     
  185.     fprintf(fout,"%s\t",basefilename);
  186.     if (type==POSTMAIL)
  187.         fprintf(fout,"mail\tbn");
  188.     if (type==POSTNEWS)
  189.         fprintf(fout,"news\tBn");
  190.     fputc(LFCHAR,fout);
  191.  
  192.     fclose (fout);
  193. }
  194.  
  195. char * DLLFUNC GetGMTime (void)
  196. {
  197. static char timebuf[40];
  198. struct tm * now;
  199. time_t zulu;
  200.     
  201.     time(&zulu);
  202.     now=gmtime(&zulu);
  203.     
  204.     wsprintf (timebuf,"%s, %d %s %04d %02d:%02d:%02d GMT",
  205.         dayofweek[now->tm_wday], now->tm_mday, months[now->tm_mon], now->tm_year+1900,
  206.         now->tm_hour, now->tm_min, now->tm_sec);
  207.         
  208.     return timebuf;
  209. }
  210.